package example2;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.junit.Test;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.*;

public class ATest {

    @Test
    public void tCSV1() throws Exception {

        List<String[]> data = new ArrayList<>();
        String[] h1 = {"维度中文名", "维度值编码", "维度值名", "备注", "上级维度值编码"};
        data.add(h1);
        data.add(h1);
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 100; j++) {
                String[] row = {"维度名" + i, "Test" + i + j, "维度值名" + j, "备注测试导入", "0"};
                data.add(row);
            }
        }
//        ExcelWriter writer = ExcelUtil.getWriter(FileUtils.getFile("C:/Users/yangyang/Desktop/TEMP/importData.csv"));
//        Iterator<String[]> iterator = data.iterator();
//        writer.write((Iterable<?>) iterator);

        CSVUtils.writeCSV("C:/Users/yangyang/Desktop/TEMP/importData.csv", data);
    }

    @Test
    public void getFileTime() throws Exception {
        File file = FileUtils.getFile("C:\\Users\\yangyang\\Desktop\\EOA");

        BasicFileAttributes attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
        FileTime fileTime = attributes.creationTime();
        FileTime modifiedTime = attributes.lastModifiedTime();

        String fileTimeStr = DateFormatUtils.format(new Date(fileTime.toMillis()), "yyyy-MM-dd HH:mm:ss");
        String modifiedTimeStr = DateFormatUtils.format(new Date(modifiedTime.toMillis()), "yyyy-MM-dd HH:mm:ss");
        System.out.println(fileTimeStr);
        System.out.println(modifiedTimeStr);

        BigInteger sizeOf = FileUtils.sizeOfAsBigInteger(file);

        System.out.println(sizeOf);
        FileUtil.size(file);
        System.out.println(FileUtil.readableFileSize(Long.valueOf(sizeOf.toString())));

    }

    @Test
    public void testLongSize() throws Exception {
        String longStr = Long.valueOf(Long.MAX_VALUE).toString();
        System.out.println(longStr);
        String displaySize = FileUtils.byteCountToDisplaySize(new BigInteger(longStr));
        System.out.println(displaySize);
        String size = FileUtil.readableFileSize(Long.valueOf(longStr));
        System.out.println(size);
    }


    @Test
    public void listAllFiles() throws Exception {
        File file = FileUtils.getFile("C:\\Users\\yangyang\\Desktop\\img");

        //获取所有子目录文件
        //recursive =true 递归所有子目录的文件 ，extensions=null 返回所有文件,递归获取所有文件
        Collection<File> listFiles = FileUtils.listFiles(file, null, true);
        int size = listFiles.size();
        System.out.println(size);


        //递归获取所有文件和目录
        Collection<File> filesAndDirs = FileUtils.listFilesAndDirs(file, TrueFileFilter.INSTANCE, DirectoryFileFilter.DIRECTORY);
        int size1 = filesAndDirs.size();
        System.out.println(size1);


        //递归获取所有目录
        Collection<File> dirs = FileUtils.listFilesAndDirs(file, new NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY);
        int size2 = dirs.size();
        System.out.println(size2);
    }


    @Test
    public void filterFiles() throws Exception {
        File rootDirectory = FileUtils.getFile("C:/");// 替换为您的根目录的路径


        //获取所有子目录文件
        //recursive =true 递归所有子目录的文件 ，extensions=null 返回所有文件,递归获取所有文件
        Collection<File> listFiles = FileUtils.listFiles(rootDirectory, null, true);

        int size = listFiles.size();
        System.out.println(size);

        String userDir = System.getProperty("user.home");
        String appData = FileUtils.getFile(userDir, "AppData").getAbsolutePath();
        String tempDir = System.getProperty("java.io.tmpdir");
        // 要排除的目录的路径
        final String[] excludedDirectories = {"C:\\Windows"
                , "C:\\PerfLogs"
                , "C:\\Recovery"
                , "C:\\Program Files"
                , "C:\\Program Files (x86)"
                , "C:\\ProgramData"
                , "C:\\IDEA"
                , "C:\\Eclipse"
                , "C:\\Applications"
                , appData
                , tempDir
        };
        IOFileFilter exclusionFilter = new IOFileFilter() {
            @Override
            public boolean accept(File file) {
                for (String excludedDir : excludedDirectories) {
                    if (file.getAbsolutePath().startsWith(excludedDir)) {
                        return false;
                    }
                }
                return true;
            }

            @Override
            public boolean accept(File dir, String name) {
                return accept(new File(dir, name));
            }
        };
        Collection<File> files = FileUtils.listFiles(rootDirectory, exclusionFilter, TrueFileFilter.INSTANCE);
        System.out.println(files.size());
        System.out.println(files.size());

    }

    @Test
    public void testDir() throws Exception {
        String uuidStr = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        System.out.println(uuidStr);

        // 获取当前用户目录
        String userDir = System.getProperty("user.home");
        System.out.println("用户目录: " + userDir);

        // 获取临时文件目录
        String tempDir = System.getProperty("java.io.tmpdir");
        System.out.println("临时文件目录: " + tempDir);

        System.out.println("当前工作路径: " + System.getProperty("user.dir"));

        System.out.println("用户的账户名称: " + System.getProperty("user.name"));
        System.out.println("java.version: " + System.getProperty("java.version"));
        System.out.println("os.name: " + System.getProperty("os.name"));
        System.out.println("java.vm.name: " + System.getProperty("java.vm.name"));

    }


    @Test
    public void writeAllFileInfo() throws Exception {
        File file = FileUtils.getFile("C:/Users/yangyang/Desktop/EOA");
        //递归获取所有文件和目录
        Collection<File> filesAndDirs = FileUtils.listFilesAndDirs(file, TrueFileFilter.INSTANCE, DirectoryFileFilter.DIRECTORY);
        int size1 = filesAndDirs.size();
        System.out.println(size1);

        List<String[]> data = new LinkedList<>();
        String[] head = {"path", "name", "type", "size", "sizeByte", "createTime", "updateTime"};
        data.add(head);
        for (File f : filesAndDirs) {
            BasicFileAttributes attributes = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
            FileTime fileTime = attributes.creationTime();
            FileTime modifiedTime = attributes.lastModifiedTime();
            String creationTimeStr = DateFormatUtils.format(new Date(fileTime.toMillis()), "yyyy-MM-dd HH:mm:ss");
            String modifiedTimeStr = DateFormatUtils.format(new Date(modifiedTime.toMillis()), "yyyy-MM-dd HH:mm:ss");

            long size = FileUtil.size(f);
            String sizes = FileUtil.readableFileSize(size);

            String[] stu0 = {f.getAbsolutePath(), f.getName(), f.isFile() ? "File" : "Dir", sizes, Long.valueOf(size).toString(), creationTimeStr, modifiedTimeStr};
            data.add(stu0);
        }
        CSVUtils.writeCSV("C:/Users/yangyang/Desktop/TEMP/yyds.test.csv", data);
        System.out.println(">>>");
    }


    @Test
    public void t2() throws Exception {

        byte[] byteArray = "byteStream".getBytes();
        ByteArrayInputStream stream = new ByteArrayInputStream(byteArray);
        long length = byteArray.length / 111;
        AudioInputStream inputStream = new AudioInputStream(stream, null, length);
        AudioSystem.write(inputStream, AudioFileFormat.Type.WAVE, new File(""));


        ByteArrayInputStream in = new ByteArrayInputStream("0".getBytes());
        in.read("1".getBytes());
        in.read("2".getBytes());
        in.read("3".getBytes());


    }


    @Test
    public void t3() throws Exception {
//C:    // C:/Users/yangyang/Desktop/科目度量.TXT
        //INSERT INTO "T_SYS_DICT" ("ID", "CODE", "NAME", "TYPE_CODE", "TYPE_NAME", "IS_SYSTEM", "IS_EFFECT", "SORT", "CREATOR_ID", "CREATE_TIME", "MODIFIER_ID", "MODIFY_TIME") VALUES ('Dict955508d6c8444b45969b169aa168c4bc', 'ORG_NO', '机构编号', 'SYS_DICT_SBJCT_MEASURE', '科目度量', '2', '1', '1', 'User5bbb8dfcc36b44f0b65b80f3d1c84a96', TO_DATE('2022-09-14 19:34:19', 'SYYYY-MM-DD HH24:MI:SS'), NULL, NULL);

        File file = FileUtils.getFile("C:/Users/yangyang/Desktop/科目度量.TXT");
        List<String> list = FileUtils.readLines(file, "UTF-8");
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            String[] split = StringUtils.split(s, ",");
            String id = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
            final String fm = "INSERT INTO \"T_SYS_DICT\" (\"ID\", \"CODE\", \"NAME\", \"TYPE_CODE\", \"TYPE_NAME\", \"IS_SYSTEM\", \"IS_EFFECT\", \"SORT\", \"CREATOR_ID\", \"CREATE_TIME\", \"MODIFIER_ID\", \"MODIFY_TIME\") VALUES ('%s', '%s', '%s', 'SYS_DICT_SBJCT_MEASURE', '科目度量', '2', '1', '%s', 'User5bbb8dfcc36b44f0b65b80f3d1c84a96', TO_DATE('2022-09-14 19:34:19', 'SYYYY-MM-DD HH24:MI:SS'), NULL, NULL);";
            String format = String.format(fm, id, split[1], split[0], i + 1);
            System.out.println(format);
        }

    }
}
